<?php  namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Loader;
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Cell;
use think\Request;
use Workerman\Worker;

class Common extends Rest
{

    public function index()
    {

    }

    /**
     *
     * 获取湖南网络工程职业学院各年级各系各专业各班
     *
     */
    public function getGrade()
    {
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
        $way = $data['way'];
        switch ($way) {
            case 1:
                $data = Db::name('clsinfo')->distinct(true)->column("grade");
                if ($data) {
                    $this->data['code'] = 1;
                    $this->data['data'] = $data;
                } else {
                    $this->data['code'] = 0;
                    $this->data['msg'] = "获取失败";
                }
                return $this->data;
                break;
            case 2:
                $grade = $data['grade'];
                $data = Db::name('clsinfo')->where(['grade' => $grade])->distinct(true)->column("department");
                if ($data) {
                    $this->data['code'] = 1;
                    $this->data['data'] = $data;
                } else {
                    $this->data['code'] = 0;
                    $this->data['msg'] = "获取失败";
                }
                return $this->data;
                break;
            case 3:
                $grade = $data['grade'];
                $department = $data['department'];
                $data = Db::name('clsinfo')->where(['grade' => $grade, 'department' => $department])->distinct(true)->column("cls");
                if ($data) {
                    $this->data['code'] = 1;
                    $this->data['data'] = $data;
                } else {
                    $this->data['code'] = 0;
                    $this->data['msg'] = "获取失败";
                }
                return $this->data;
        }
    }


    public function excleFlieUpload()
    {
        header("content-type:text/html;charset=utf-8");
        //�ϴ�excel�ļ�
        $file = request()->file('file');
        //�Ƶ�/public/uploads/excel/��
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel');
        if ($info) {
            //����PHPExcel��
            vendor('PHPExcel.PHPExcel.Reader.Excel2007');
            //��ȡ�ϴ�����ļ���
            $filePath = "uploads/excel/" . str_replace('\\', '/', $info->getSaveName());
            //ʵ��PHPExcel��
            $PHPReader = new \PHPExcel_Reader_Excel2007();
            //��ȡexcel�ļ�
            $objPHPExcel = $PHPReader->load($filePath);
            //��ȡexcel�ļ��еĵ�һ��������
            $sheet = $objPHPExcel->getSheet(0);
            $allRow = $sheet->getHighestRow();  //ȡ��������
            //$allColumn = $sheet->getHighestColumn();���
            echo $allRow . "dsadsa";
            for ($j = 5; $j <= $allRow - 1; $j++) {
                if ($objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue() != "") {
                    $data['Name'] = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();
                    $data['Sex'] = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue();
                    $data['Birthday'] = $objPHPExcel->getActiveSheet()->getCell("D" . $j)->getValue();
                    $data['StudentID'] = $objPHPExcel->getActiveSheet()->getCell("E" . $j)->getValue();
                    $data['IdCard'] = $objPHPExcel->getActiveSheet()->getCell("F" . $j)->getValue();
                    $data['Telephone'] = $objPHPExcel->getActiveSheet()->getCell("G" . $j)->getValue();
                    $data['Grade'] = $objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue();
                    $data['Department'] = $objPHPExcel->getActiveSheet()->getCell("I" . $j)->getValue();
                    $data['Class'] = $objPHPExcel->getActiveSheet()->getCell("J" . $j)->getValue();
                    $data['DormitoryId'] = $objPHPExcel->getActiveSheet()->getCell("K" . $j)->getValue();
                    $last_id = Db::name('userinfo')->insertGetId($data);//�
                }
                if ($last_id) {
                    echo "第" . $j . "成功添加users:" . $last_id . "�<br/>";
                } else {
                    echo "第" . $j . "�失败<br/>";
                }
            }
        } else {
            echo "文件上传失败";
        }
    }

    //交流合作
    public function Cooperation(){
        if (1 != self::check_power(1)) {
            return $this->data;
        }
        $request = Request::instance();
        $data = $request->param();
        $name = $data['Name'];
        $telephone = $data['Telephone'];
        $VcCode = $data['VcCode'];
        $email = $data['Email'];
        $content = $data['Content'];
        $query_re = Db::name('cooperation')->where(['Name'=>$name,'Telephone'=>$telephone])->find();
        if($query_re){
            $this->data['code'] = 0;
            $this->data['msg']  = '您已经提交过，请勿重复提交';
        }else{
            $data = [
                'Name' => $name,
                'Telephone' => $telephone,
                'Email' => $email,
                'Content' => $content,
                'DateTime' => date("Y-m-d H:i:s", time()),
                'Data_stu' =>1
            ];
            $result = Db::name('cooperation')->insertGetId($data);
            if($result){
                $this->data['code'] = 1;
                $this->data['msg']  = '提交成功';
            }else{
                $this->data['code'] = 0;
                $this->data['msg']  = '提交失败';
            }
        }
        return $this->data;
     }

    //WebSocket
    public function WebSocket(){
        require_once '../../../vendor/workerman/workerman-for-win/Autoloader.php';
        $wx_worker = new Worker('websocket://127.0.0.1:2000');
        $wx_worker->count = 4;
        $wx_worker->onMessage = function($connection,$data){
          $connection->send("Hello".$data);
        };
    }
}